<?php

require_once(DIR_SYSTEM . 'engine/restcontroller.php');

class ControllerManagerStock extends RestController {
	
	//»ñÈ¡ÒµÎñÔ±È«²¿³ö¿âµ¥
	public function getStockIn(){

		// echo 1111111;
		
		$this->checkPlugin();
		$this->load->model('manager/stock');
		
		$filter['start'] = 0;

		if(isset($this->request->get['limit'])){
			$filter['limit'] = $this->request->get['limit'];
		}
		else{
			$filter['limit'] = 10;
		}

		$json = array('success' => true);

		$customer_id = $this->customer->getId();
		
		//È¡ÒµÎñÔ±µÄ²Ö¿â
		$this->load->model('account/customer');

		$tel = $this->model_manager_stock->getTel($customer_id);
		$logcenter = $this->model_account_customer->userCheckByTelephone($tel);

		$filter['logcenter_id'] = $logcenter['MG_logcenterId'];

		$results = $this->model_manager_stock->getStockIn($filter);
		foreach($results as $v){

			switch($v['tab']){
				case 'op':
					$json['inList']['op'][] = $v;
					$json['opCount'] ++;
					break;
				case 'cancel':
					$json['inList']['cancel'][] = $v;
					$json['cancelCount'] ++;
					break;
				case 'all':
					$json['inList']['all'][] = $v;
					$json['allCount'] ++;
					break;
				default;
					break;
			}
		
		}

		$this->response->setOutput(json_encode($json));

	}
	
	//¼ÓÔØ¸ü¶àµ¥¾Ý
	public function loadMoreIn(){

		$json = array('success' => true);

		$json['count'] = 0;
		
		$this->checkPlugin();
		$this->load->model('manager/stock');

		if(isset($this->request->get['status']) && $this->request->get['status'] != 'all'){
			$filter['status'] = $this->request->get['status'];
		}

		if(isset($this->request->get['tab'])){
			$filter['tab'] = $this->request->get['tab'];
		}

		if(isset($this->request->get['limit'])){
			$filter['limit'] = $this->request->get['limit'];
		}
		else{
			$filter['limit'] = 10;
		}
		
		if(isset($this->request->get['start'])){
			$filter['start'] = $this->request->get['start'];
		}

		$results = $this->model_manager_stock->loadMoreIn($filter);

		foreach($results as $v){

			$json['inList'][$filter['tab']][] = $v;
			$json['count'] ++;
		
		}

		$this->response->setOutput(json_encode($json));

	}
	
	//µã»÷ÅäËÍ°´Å¥
	public function checkSend(){
		
		$this->checkPlugin();
		$this->load->model('manager/stock');

		$requestjson = file_get_contents('php://input');
		$data = json_decode($requestjson, true);

		$message = $this->model_manager_stock->checkSend($data);

		$json = array(
			'success' => true,	
			'message' => $message
		);
		$this->response->setOutput(json_encode($json));

	}

	//µã»÷Ç©ÊÕ°´Å¥
	public function checkSign(){
		
		$this->checkPlugin();
		$this->load->model('manager/stock');

		$requestjson = file_get_contents('php://input');
		$data = json_decode($requestjson, true);

		$message = $this->model_manager_stock->checkSign($data);

		$json = array(
			'success' => true,	
			'message' => $message
		);
		$this->response->setOutput(json_encode($json));

	}

}

?>